﻿using DemountCIM.Framework.DataBase.Table;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WFDataBase;

namespace DemountCIM.Framework.DataBase
{
    public class ParameterRepository : BaseRepository<TbParameter>, IParameterRepository
    {
        private int sort = 0;

        public ParameterRepository(SqlSugarScope context) : base(context)
        {
        }


        public List<TbParameter> GetAllParameters(int runId)
        {
            var list = AsQueryable().Where(a => a.RunId == runId).ToList();

            return list;
        }

        public void InsertPrivateParam(int runId, string key, string name, string description, ParamType paraType, UserLevel paraLevel = UserLevel.操作工, bool readOnly = false, bool visible = true)
        {
            var param = AsQueryable().Where(a => a.RunId == runId && a.ParamKey == key).First();

            if (param == null)
            {
                Insert(new TbParameter
                {
                    Description = description,
                    ParamKey = key,
                    ParamName = name,
                    ParamType = paraType,
                    ReadOnly = readOnly,
                    RunId = runId,
                    UserLevel = paraLevel,
                    Visible = visible,
                    Sort = sort
                });
            }
            else
            {
                if (param.ParamName != name)
                {
                    AsUpdateable().Where(a => a.RunId == runId && a.ParamKey == key).SetColumns(a => a.ParamName, name).ExecuteCommand();
                }
                if (param.Description != description)
                {
                    AsUpdateable().Where(a => a.RunId == runId && a.ParamKey == key).SetColumns(a => a.Description, description).ExecuteCommand();
                }
                if (param.Sort != sort)
                {
                    AsUpdateable().Where(a => a.RunId == runId && a.ParamKey == key).SetColumns(a => a.Sort, sort).ExecuteCommand();
                }
            }

            sort++;
        }

        public bool ReadBool(string paramKey, bool defaultValue)
        {
            TbParameter parameter = GetSingle(a => a.ParamKey == paramKey);
            if (parameter == null)
            {
                return defaultValue;
            }
            bool.TryParse(parameter.ParamValue, out var value);
            return value;
        }

        public int ReadInt(string paramKey, int defaultValue)
        {
            TbParameter parameter = GetSingle(a => a.ParamKey == paramKey);
            if (parameter == null || string.IsNullOrEmpty(parameter.ParamValue))
            {
                return defaultValue;
            }

            int.TryParse(parameter.ParamValue, out var value);
            return value;
        }

        public string ReadString(string paramKey, string defaultValue)
        {
            TbParameter parameter = GetSingle(a => a.ParamKey == paramKey);
            if (parameter == null || string.IsNullOrEmpty(parameter.ParamValue))
            {
                return defaultValue;
            }
            return parameter.ParamValue;
        }

        public bool WriteBool(int runId, string paramKey, bool value)
        {
            int resCount = AsUpdateable().Where(a => a.RunId == runId && a.ParamKey == paramKey).SetColumns(a => a.ParamValue, value.ToString()).ExecuteCommand();
            return resCount > 0;
        }

        public bool WriteInt(int runId, string paramKey, int value)
        {
            int resCount = AsUpdateable().Where(a => a.RunId == runId && a.ParamKey == paramKey).SetColumns(a => a.ParamValue, value).ExecuteCommand();
            return resCount > 0;
        }

        public bool WriteString(int runId, string paramKey, string value)
        {
            int resCount = AsUpdateable().Where(a => a.RunId == runId && a.ParamKey == paramKey).SetColumns(a => a.ParamValue, value).ExecuteCommand();
            return resCount > 0;
        }
    }
}
